package com.storage.memory;

import com.wink.charity.Charity;
import com.wink.dt.Topic;
import com.wink.event.Event;

import java.util.ArrayList;
import java.util.HashMap;

public class StorageMemory {
	
	private HashMap<String, ArrayList<Charity>> charityData = new HashMap<String, ArrayList<Charity>>();
	
	private HashMap<String, ArrayList<Event>> eventData = new HashMap<String, ArrayList<Event>>();
	private HashMap<String, ArrayList<Topic>> topicData = new HashMap<String, ArrayList<Topic>>();

	
	/**
	 * add a new Charity
	 * 
	 * @param Charity
	 * @return
	 */
	public Charity add(Charity charity) {
		if (charity == null)
			return null;
		
		// TODO better ID generation
		//charity.setId(eventData.size() + 1);

		ArrayList<Charity> set = charityData.get(charity.getName());
		if (set == null) {
			set = new ArrayList<Charity>();
			charityData.put(charity.getName(), set);
		}
		set.add(charity);
		return charity;
	}	

	/**
	 * all events
	 * 
	 * @return
	 */
	public ArrayList<Charity> getCharities() {
		ArrayList<Charity> all = new ArrayList<Charity>();
		for (String k : charityData.keySet())
			all.addAll(charityData.get(k));

		return all;
	}
	
	
	/**
	 * add a new event
	 * 
	 * @param event
	 * @return
	 */
	public Event add(Event event) {
		if (event == null)
			return null;

		if (event.getSpace() == null)
			event.setSpace("undefined");

		// TODO better ID generation
		event.setId(eventData.size() + 1);

		ArrayList<Event> set = eventData.get(event.getSpace());
		if (set == null) {
			set = new ArrayList<Event>();
			eventData.put(event.getSpace(), set);
		}
		set.add(event);
		return event;
	}
	

	/**
	 * all events
	 * 
	 * @return
	 */
	public ArrayList<Event> getEvents() {
		ArrayList<Event> all = new ArrayList<Event>();
		for (String k : eventData.keySet())
			all.addAll(eventData.get(k));

		return all;
	}
	

	/**
	 * add a new topic
	 * 
	 * @param topic
	 * @return
	 */
	public Topic add(Topic topic) {
		if (topic == null)
			return null;			

		ArrayList<Topic> set = topicData.get(topic.getTopicTitle());
		if (set == null) {
			set = new ArrayList<Topic>();
			topicData.put(topic.getTopicTitle(), set);
		}
		set.add(topic);
		return topic;
	}
	
	/**
	 * all topics
	 * 
	 * @return
	 */
	public ArrayList<Topic> getTopics() {
		ArrayList<Topic> all = new ArrayList<Topic>();
		for (String k : topicData.keySet())
			all.addAll(topicData.get(k));

		return all;
	}
}
